<template>
  <div class="register_page fillcontain">
    <transition name="form-fade" mode="in-out">
      <section class="form_container" v-show="showRegister">
        <div class="manage_tip">
          <p>注册</p>
        </div>
        <el-form :model="registerForm" :rules="rules" ref="registerForm">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="用户名"><span>dsfsf</span></el-input>
          </el-form-item>
          <el-form-item prop="countryCode">
            <el-input v-model="registerForm.countryCode" placeholder="国家区号"><span>+86</span></el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input v-model="registerForm.phone" placeholder="手机号"><span>15576643057</span></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="密码" v-model="registerForm.password" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="checkPassword">
            <el-input type="password" placeholder="确认密码" v-model="registerForm.checkPassword" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('registerForm')" class="submit_btn">注册</el-button>
            <el-button @click="restForm('registerForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </section>
    </transition>
  </div>
</template>

<script>
import {register} from '@/api/getData'
import {mapActions} from 'vuex'
import {isCountryCode, isUsername, isEmail, isPhone, isChinaPhone} from '@/utils/validate'

export default {
  data () {
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else if (!isUsername(value)) {
        callback(new Error('用户名格式不对，只支持字母和数字'))
      } else {
        callback()
      }
    }

    var validateCountryCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入国家区号'))
      } else if (!isCountryCode(value)) {
        callback(new Error('请输入正确的国家区号'))
      } else {
        callback()
      }
    }

    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (this.$ref.registerForm.countryCode === '+86' && !isChinaPhone(value)) {
        callback(new Error('手机号格式不对'))
      } if (!isPhone(value)) {
        callback(new Error('手机号格式不对，只支持纯数字'))
      } else {
        callback()
      }
    }

    var validateEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else if (!isEmail(value)) {
        callback(new Error('邮箱格式不对'))
      } else {
        callback()
      }
    }

    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.$ref.registerForm.checkPassword !== '') {
          this.$ref.registerForm.validateField('checkPassword')
        }
        callback()
      }
    }
    var validateCheckPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        countryCode: '',
        phone: '',
        email: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, validator: validateUsername, trigger: 'blur' }
        ],
        countryCode: [
          {required: true, validator: validateCountryCode, trigger: 'blur'}
        ],
        phone: [
          {required: true, validator: validatePhone, trigger: 'blur'}
        ],
        email: [
          {required: true, validator: validateEmail, trigger: 'blur'}
        ],
        password: [
          { required: true, validator: validatePassword, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, validator: validateCheckPassword, trigger: 'blur' }
        ]
      },
      showRegister: true
    }
  },
  methods: {
    ...mapActions(['getAdminData']),
    async submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await register({user_name: this.loginForm.username, password: this.loginForm.password})
          if (res.status === 1) {
            this.$message({
              type: 'success',
              message: '登录成功'
            })
            this.$router.push('manage')
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        } else {
          this.$notify.error({
            title: '错误',
            message: '请输入正确的用户名密码',
            offset: 100
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
  @import '../style/mixin.less';
  .register_page{
    background-color: #324057;
  }
  .manage_tip{
    position: absolute;
    width: 100%;
    top: -100px;
    left: 0;
  }
  p{
    font-size: 34px;
    color: #fff;
  }
  .form_container{
  .wh(320px, 210px);
  .ctp(320px, 210px);
    padding: 25px;
    border-radius: 5px;
    text-align: center;
    background-color: #fff;
  }
  /*.submit_btn{*/
    /*width: 100%;*/
    /*font-size: 16px;*/
  /*}*/
  .form-fade-enter-active, .form-fade-leave-active {
    transition: all 1s;
  }
  .form-fade-enter, .form-fade-leave-active {
    transform: translate3d(0, -50px, 0);
    opacity: 0;
  }
</style>
